package com.ruoyi.web.controller.business.rule;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ruoyi.business.base.PageParam;
import com.ruoyi.business.rule.domain.JbLogisticsRules;
import com.ruoyi.business.rule.service.JbLogisticsRulesService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.web.core.JoR;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.Arrays;
import java.util.List;

/**
 * 物流规则 Controller
 *
 * @author ruoyi
 * @date 2024-10-11
 */
@Api("物流规则")
@RestController
@RequestMapping("/logistics_rules")
public class JbLogisticsRulesController extends BaseController
{
    @Autowired
    private JbLogisticsRulesService service;

    /**
     * 查询物流规则列表
     */
    @ApiOperation("查询物流规则列表")
    @GetMapping("/list")
    public TableDataInfo list(PageParam page, JbLogisticsRules jbLogisticsRules)
    {
        LambdaQueryWrapper<JbLogisticsRules> select = new LambdaQueryWrapper<>();
        select.eq(JbLogisticsRules::isStatus, true);
        IPage<JbLogisticsRules> iPage = service.page(page.b(), select);
        return JoR.rows(iPage);
    }

    /**
     * 导出物流规则列表
     */
    @ApiOperation("导出物流规则列表")
    @Log(title = "物流规则", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PageParam page, JbLogisticsRules jbLogisticsRules)
    {
        LambdaQueryWrapper<JbLogisticsRules> select = new LambdaQueryWrapper<>();
        IPage<JbLogisticsRules> iPage = service.page(page.b(), select);

        List<JbLogisticsRules> list = iPage.getRecords();
        ExcelUtil<JbLogisticsRules> util = new ExcelUtil<JbLogisticsRules>(JbLogisticsRules.class);
        util.exportExcel(response, list, "物流规则数据");
    }

    /**
     * 获取物流规则详细信息
     */
    @ApiOperation("获取物流规则详细信息")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        JbLogisticsRules rules = service.getById(id);
        return success(rules);
    }

    /**
     * 新增物流规则
     */
    @ApiOperation("新增物流规则")
    @PreAuthorize("@ss.hasPermi('logistics_rules:change')")
    @Log(title = "物流规则", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Valid @RequestBody JbLogisticsRules jbLogisticsRules)
    {
        return toAjax(service.save(jbLogisticsRules));
    }

    /**
     * 修改物流规则
     */
    @ApiOperation("修改物流规则")
    @PreAuthorize("@ss.hasPermi('logistics_rules:change')")
    @Log(title = "物流规则", businessType = BusinessType.UPDATE)
    @PutMapping()
    public AjaxResult edit(@Valid @RequestBody JbLogisticsRules jbLogisticsRules)
    {
        return toAjax(service.updateById(jbLogisticsRules));
    }

    /**
     * 修改物流规则 状态
     */
    @ApiOperation("修改物流规则 状态")
    @PreAuthorize("@ss.hasPermi('logistics_rules:change')")
    @Log(title = "物流规则 状态", businessType = BusinessType.UPDATE)
    @PutMapping("/putStatus")
    public AjaxResult putStatus(@RequestBody JbLogisticsRules jbLogisticsRules)
    {
        return toAjax(service.updateById(jbLogisticsRules));
    }

    /**
     * 删除物流规则
     */
    @ApiOperation("删除物流规则")
    @PreAuthorize("@ss.hasPermi('logistics_rules:change')")
    @Log(title = "物流规则", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(service.removeByIds(Arrays.asList(ids)));
    }
}
